package org.kevin.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.kevin.aop.Log;
import org.kevin.entity.SysLog;
import org.kevin.service.SysLogService;
import org.kevin.utils.HttpContextUtils;
import org.kevin.utils.IPUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * @ClassName: LogAspect
 * @Description: TODO
 * @Author: kevinHan
 * @Date: 2019/8/27 23:09
 * @Version: 1.0
 */
@Aspect
@Component
public class LogAspect {

    @Autowired
    SysLogService sysLogService;

    @Pointcut("@annotation(org.kevin.aop.Log)")
    public void pointcut() {}

    @Around("pointcut()")
    public Object arounnd(ProceedingJoinPoint point) {
        Object result = null;
        long beginTime = System.currentTimeMillis();
        try {
            /** 执行方法 */
            result = point.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        /** 执行时长(毫秒)*/
        long time = System.currentTimeMillis() - beginTime;
        /** 保存日志*/
        saveLog(point, time);
        return result;
    }

    private void saveLog(ProceedingJoinPoint joinPoint, long time) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        SysLog sysLog = new SysLog();
        /** 获得注解中所有的内容对象 */
        Log logAnnotation = method.getAnnotation(Log.class);
        if (logAnnotation != null) {
            /** 获得描述的值 */
            sysLog.setOperation(logAnnotation.value());
        }
        /** 请求的方法名 */
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = signature.getName();
        sysLog.setMethod(className + "." + methodName + "()");
        /** 请求的方法的参数 */
        Object[] args = joinPoint.getArgs();
        /** 请求的方法参数值 */
        LocalVariableTableParameterNameDiscoverer nameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
        String[] parameterNames = nameDiscoverer.getParameterNames(method);
        if (args != null && parameterNames != null) {
            String params = "";
            for (int i = 0; i < args.length; i++) {
                params += " " + parameterNames[i] + ": " + args[i];
            }
            sysLog.setParams(params);
        }
        /** 获取request */
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        /** 设置IP地址 */
        sysLog.setIp(IPUtils.getIpAddr(request));
        /** 模拟一个用户 */
        sysLog.setUserName("kevin");
        sysLog.setTime((int) time);
        sysLog.setCreateTime(new Date());
        /** 保存系统日志 */
        sysLogService.saveSysLog(sysLog);
    }
}
